package android.olsson.apk;

import java.util.ArrayList;
import java.util.Scanner;

import android.olsson.apk.interfaces.IBeverage;
import android.olsson.apk.interfaces.IDatabaseHandler;
import android.olsson.apk.interfaces.IFileHandler;

public class DatabaseHandler implements IDatabaseHandler {
	private Scanner scanner;

	public DatabaseHandler() {
		IFileHandler fileHandler = new FileHandler();
		scanner = new Scanner(fileHandler.readFile());
	}

	public ArrayList<IBeverage> getBeveragesByName(String beverageName) {
		ArrayList<IBeverage> beverageList = new ArrayList<IBeverage>();
		while (scanner.hasNext()) {
			String name = scanner.nextLine();
			String type = scanner.nextLine();
			float volume = Float.valueOf(scanner.nextLine());
			float alc = Float.valueOf(scanner.nextLine());
			float price = Float.valueOf(scanner.nextLine());
			float apk = Float.valueOf(scanner.nextLine());
			if (beverageName != null) {
				if (name.toLowerCase().contains(beverageName.toLowerCase())) {
					beverageList.add(new Beverage(name, type, volume, alc, price, apk));
				}
			}
		}
		return beverageList;
	}

	public ArrayList<IBeverage> getBeveragesByCondition(String typeToShow,
			int itemsToShow) {
		ArrayList<IBeverage> beverageList = new ArrayList<IBeverage>();
		int index = 0;
		while (scanner.hasNext()) {
			String name = scanner.nextLine();
			String type = scanner.nextLine();
			float volume = Float.valueOf(scanner.nextLine());
			float alc = Float.valueOf(scanner.nextLine());
			float price = Float.valueOf(scanner.nextLine());
			float apk = Float.valueOf(scanner.nextLine());
			if ((typeToShow.equalsIgnoreCase(type) || typeToShow
					.equalsIgnoreCase("Alla")) && (index < itemsToShow)) {
				beverageList.add(new Beverage(name, type, volume, alc, price, apk));
				++index;
			}
		}
		return beverageList;
	}
}
